package com.magical.controller;

import com.magical.entity.RestJson;
import com.magical.entity.User;
import com.magical.service.FilesService;
import com.magical.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private FilesService filesService;

    @RequestMapping("/exist")
    @ResponseBody
    public String exist(HttpServletRequest request) {
        String em = request.getParameter("e");
        String pw = request.getParameter("p");
        User user = userService.findByEmail(em);
        if (user == null)
            return "false";
        else {
            if (pw.equals(user.getPasswd())){
                HttpSession session = request.getSession();
                session.setAttribute("loginUser", user);
                return "success";
            }
            else
                return "false";
        }
    }


    @RequestMapping("/checkUser")
    @ResponseBody
    public RestJson<User> checkUser(HttpServletRequest request) {
        String em = request.getParameter("e");
        String pw = request.getParameter("p");
        User user = userService.findByEmail(em);
        RestJson<User> result=new RestJson<>();
        if (user == null){
            result.setCode(-1);
            result.setMessage("该用户不存在");
            return result;
        }

        else {
            if (pw.equals(user.getPasswd())){
                result.setData(user);
                return result;
            }
            else{
                result.setCode(1);
                result.setMessage("用户密码错误");
                return result;
            }

        }
    }

    @RequestMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        session.invalidate();
        try {
            response.sendRedirect("/login.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/register")
    @ResponseBody
    public String register(HttpServletRequest request) {
        String name = request.getParameter("n");
        String email = request.getParameter("e");
        String password = request.getParameter("p");
        int user = userService.addUser(name,email,password);
        if(user>0){
            filesService.createUser(user);
            return "success";
        }else
            return "false";
    }

    @RequestMapping("/checkEmail")
    @ResponseBody
    public int checkEmail(HttpServletRequest request) {
        String email = request.getParameter("e");
        User user = userService.findByEmail(email);
        if(user == null)
            return 1;
        else
            return 0;
    }

    @RequestMapping("/selectAllUserMessage")
    @ResponseBody
    public List<User> getAllUsers(){
        //
        return userService.selectAllUserMessage();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("id"));
        if(userService.delete(id)==1){
            return"success";
        }else{
            return "failed";
        }
    }

    @RequestMapping("/findUserById")
    @ResponseBody
    public User find(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("id"));
        return userService.find(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("i"));
        String email=request.getParameter("e");
        int authority=Integer.parseInt(request.getParameter("a"));
        int grade=Integer.parseInt(request.getParameter("g"));
        if(userService.update(id,email,authority,grade)==1){
            return "success";
        } else {
            return "false";
        }
    }

    @RequestMapping("/showUsedSpace")
    @ResponseBody
    public Map<String,String> showUsedSpace(HttpServletRequest request){
        HttpSession session = request.getSession();
        int userId = ((User)session.getAttribute("loginUser")).getId();
        User u=userService.find(userId);
        String usedSize=u.getUserUsedSpace();
        String maxSize=String.valueOf(userService.maxSpace(u.getGradeId()))+"GB";
        Map<String,String> map= new HashMap<>();
        map.put("usedSpace",usedSize);
        map.put("maxSpace",maxSize);
        return map;
    }
}